package org.sgx.gwteditors.client.impl1.simple;

import java.util.Date;
import java.util.List;

import org.sgx.gwteditors.client.editor.Editor;
import org.sgx.gwteditors.client.editor.error.EditorError;
import org.sgx.gwteditors.client.editor.error.EditorErrorManager;
import org.sgx.gwteditors.client.editor.event.EditorEventManager;
import org.sgx.gwteditors.client.editor.event.ValueChangeListener;
import org.sgx.gwteditors.client.impl1.EditorFramework1;
import org.sgx.gwteditors.client.impl1.EditorManager1;
import org.sgx.gwteditors.client.impl1.basetypes.Time;

import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.Widget;
/**
 * the same editor impl of TimeEdtor but in this case the type edited is Java.util.Date and not the artificial Time. 
 * @author sg
 *
 */
public class TimeJavaDateEditor extends FlowPanel implements Editor<Date>{
	private TimeEditor timeEd;
	private Date model;

	@Override
	public boolean isSimple() {
		return true;
	}
	public TimeJavaDateEditor() {
		super();
//		addValueChangeHandler(this);
		timeEd = new TimeEditor();
		add(timeEd.getWidget()); 
	}
	@Override
	public Date flush() {
		if(model==null)
			return null; //TODO: log error - load() must be called first
		Time time = timeEd.flush();
		if(time==null)
			return null;
		return time.flush(model);		
	}

	@Override
	public void load(Date t) {
		model = t;
		Time time = new Time().load(model); 
		timeEd.load(time); 
	}
	@Override
	public Widget getWidget() {
		return this;
	}
	
	@Override
	public void addValueChangeListener(ValueChangeListener<Date> l) {
		EditorEventManager.<Date>getInstance().addValueChangeListener(this, l);
	}

	@Override
	public void removeValueChangeListener(ValueChangeListener<Date> l) {
		EditorEventManager.<Date>getInstance().removeValueChangeListener(this, l);
	}
	@Override
	public List<EditorError<Date>> getErrors() {
		return EditorErrorManager.<Date>getInstance().getErrors(this);
	}
	
	@Override
	public String getEditorId() {
		return EditorManager1.getInstance().getEditorName(this);		
	}

	@Override
	public String[] getEditorTypesSupported() {
		return new String[]{EditorFramework1.TYPE_DATE}; 
	}
}
